﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.ComponentModel;
using System.Windows.Threading;
using BrainTechLLC;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using ImageAppDataModels;
using BrainTechLLC.WPF;

namespace ImageAppViewModels
{
    public class FileImageCollectionStatsViewModel : BaseNotifyPropertyChangedUI
    {        
        DispatcherTimer _timer;
        protected bool _needsUpdating;

        public FileImageCollectionStatsViewModel(FileImageCollectionViewModel vm)
        {
            _vm = vm;
            AllStats.GetInstance().PropertyChanged += new PropertyChangedEventHandler(FileImageCollectionStatsViewModel_PropertyChanged);
            _timer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 2) };
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Start();
        }

        void FileImageCollectionStatsViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnPropertyChanged(e.PropertyName);
        }

        public int BackgroundCalcCounter
        {
            get { return BackgroundCalcHandler.Counter.Count; }
        }

        public int MaxCachedImageSources
        {
            get { return Settings.Values.MaxImageSourcesInMemory; }
        }

        public int CurrentBlurCounter
        {
            get { return BackgroundCalcHandler.CurrentBlurs.Count; }
        }

        public string TotalBytesRead
        {
            get { return string.Format("{0:N0}K", (double)FileImage.TotalBytesRead.Count / 1024D); }
        }

        public int ThumbsReadFromDisk
        {
            get { return ThumbnailProvider.ThumbsReadFromDisk.Count; }
        }

        public int ThumbsReadFromImageBytes
        {
            get { return ThumbnailProvider.ThumbsReadFromImageBytes.Count; }
        }

        public string TotalDrawnPixels
        {
            get { return string.Format("{0:N0}", (double)ImageOperators.TotalDrawnPixels.Count / 100000D); }
        }

        public int TotalCyclesMissedDueToFileLock
        {
            get { return FileLocking.TotalCyclesMissedDueToFileLock.Count; }
        }

        public int ImageDrawFailures
        {
            get
            {
#if COUNT_DRAW_OPERATIONS
                return ImageOperators.DrawImageFailures.Count;
#else
                return 0;
#endif
            }
        }

        public double AverageImageSourceTime
        {
            get { return AllStats.GetInstance().GetAverage("AverageImageSourceTime").AverageValue; }
        }

        public double AverageFilterTime
        {
            get { return AllStats.GetInstance().GetAverage("AverageFilterTime").AverageValue; }
        }

        public double AverageDrawImageTime
        {
            get { return AllStats.GetInstance().GetAverage("AverageDrawImageTime").AverageValue; }
        }

        public double AverageThumbLoadTime
        {
            get { return AllStats.GetInstance().GetAverage("AverageThumbLoadTime").AverageValue; }
        }

        public int LoadedByteCount
        {
            get { return _vm.LoadedByteCount; }
        }

        public string LoadedByteCountString
        {
            get { return string.Format("{0:N0}K", _vm.LoadedByteCount / 1024); }
        }

        public int LoadedFileCount
        {
            get { return _vm.LoadedFileCount; }
        }

        public int MaxFilesInMemory
        {
            get { return Settings.Values.MaxFileBytesInMemory; }
        }

        public int MaxBitmapsInMemory
        {
            get { return Settings.Values.MaxBitmapsInMemory; }
        }

        public int MaxThumbsInMemory
        {
            get { return Settings.Values.MaxThumbsInMemory; }
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            try
            {
                if (this._vm != null && this._vm.ImageCollection != null)
                {
                    this._vm.ImageCollection.GetCounts();
                }
                OnPropertyChanged((string)null);             
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

        }

        protected FileImageCollectionViewModel _vm;

        public FileImageCollectionViewModel VM
        {
            get { return _vm; }
            set { _vm = value; }
        }

        public void Invalidate()
        {
            _needsUpdating = true;
        }

        public int LoadedThumbs
        {
            get { return _vm.LoadedThumbs; }
            set { OnPropertyChanged("LoadedThumbs"); }
        }

        public int CachedImageSources
        {
            get { return _vm.CachedImageSources; }
            set { OnPropertyChanged("CachedImageSources"); }
        }

        public int CachedThumbnailImageSources
        {
            get { return _vm.CachedThumbnailImageSources; }
            set { OnPropertyChanged("CachedThumbnailImageSources"); }
        }

        public int LoadedAdditionalInfoCount
        {
            get { return _vm.LoadedAdditionalInfoCount; }
            set { OnPropertyChanged("LoadedAdditionalInfoCount"); }
        }

        public int LoadedBitmaps
        {
            get { return _vm.LoadedBitmaps; }
            set { OnPropertyChanged("LoadedBitmaps"); }
        }

        public long UnusedByteCount
        {
            get { return _vm.UnusedByteCount; }
            set { OnPropertyChanged("UnusedByteCount"); }
        }

        public string UnusedByteCountString
        {
            get { return string.Format("{0:N0}K", _vm.UnusedByteCount / 1024); }
            set { OnPropertyChanged("UnusedByteCountString"); }
        }

        public int FileImageCollectionCount
        {
            get { return _vm.FileImageCollectionCount; }
            set { OnPropertyChanged("FileImageCollectionCount"); }
        }

        public int NumberVisible
        {
            get { return _vm.NumberVisible; }
            set { OnPropertyChanged("NumberVisible"); }
        }

        public int FileImageViewModelCollectionCount
        {
            get { return _vm.FileImageViewModelCollectionCount; }
            set { OnPropertyChanged("FileImageViewModelCollectionCount"); }
        }
    }
}
